/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#ifndef SX_RENDERER_BASE_H
#define SX_RENDERER_BASE_H

#include <memory>

namespace Sx {
namespace Graphics {
namespace Rendering {

/*
 * The AbstractRenderer class provides the base of a hierarchy that extensively
 * utilizes static polymorphism (CRTP) to provide a simple render function that
 * always provides the derived type in the functions signature. This is because
 * objects in a hierarchy that are rendered typically need access to the
 * additional members defined in the derived class. For example, consider two
 * rendering functions: void render(Object* o); and void render(Shape* s); and
 * furthermore assume Shape extends Object. The second rendering function
 * provides all of the additional Shape members without requiring a dynamic_cast
 *
 * RendererImplementationType - Class that extends AbstractRenderer that
 * provides the implementation of the construct and render functions.
 * 
 * BaseObjectType - Represents the base class of the object hierarchy to be
 * rendered.
 * 
 * PhysicalRepresentationType - Defines the physical parameters of the object
 * to be rendered (vertices, or may contain additional data).
 */
template <
	typename RendererImplementationType,
	typename BaseGraphicsContextType,
	typename BaseObjectType,
	typename BasePhysicalRepresentationType
	>
class RendererBase {
public:
	RendererBase(const std::shared_ptr<BaseGraphicsContextType>& graphicsContext);
	virtual ~RendererBase();

	bool callConstruct(BaseObjectType* const object, BasePhysicalRepresentationType* const physicalRepresentation);

	void callUpdate(BaseObjectType* const object, float dt);

	/* 
	 * Call the appropriate render function for the provided object. This
	 * function is redirected to a function with a signature matching the
	 * type of object passed - ensuring the object will not have to be casted
	 * to its proper derived type during the render function call.
	 */
	void callRender(BaseObjectType* const object, BasePhysicalRepresentationType* const physicalRepresentation);

protected:
	std::shared_ptr<BaseGraphicsContextType> graphicsContext;
};

}

}

}

template <
	typename RendererImplementationType,
	typename BaseGraphicsContextType,
	typename BaseObjectType,
	typename BasePhysicalRepresentationType
	>
Sx::Graphics::Rendering::RendererBase<RendererImplementationType, BaseGraphicsContextType, BaseObjectType, BasePhysicalRepresentationType>::RendererBase(const std::shared_ptr<BaseGraphicsContextType>& graphicsContext) {
	this->graphicsContext = graphicsContext;
}

template <
	typename RendererImplementationType,
	typename BaseGraphicsContextType,
	typename BaseObjectType,
	typename BasePhysicalRepresentationType
	>
Sx::Graphics::Rendering::RendererBase<RendererImplementationType, BaseGraphicsContextType, BaseObjectType, BasePhysicalRepresentationType>::~RendererBase() {}

template <
	typename RendererImplementationType,
	typename BaseGraphicsContextType,
	typename BaseObjectType,
	typename BasePhysicalRepresentationType
	>
bool Sx::Graphics::Rendering::RendererBase<RendererImplementationType, BaseGraphicsContextType, BaseObjectType, BasePhysicalRepresentationType>::callConstruct(BaseObjectType* const object, BasePhysicalRepresentationType* const physicalRepresentation) {
	return (static_cast<RendererImplementationType*>(this))->construct(this->graphicsContext.get(), object, physicalRepresentation);
}

template <
	typename RendererImplementationType,
	typename BaseGraphicsContextType,
	typename BaseObjectType,
	typename BasePhysicalRepresentationType
	>
void Sx::Graphics::Rendering::RendererBase<RendererImplementationType, BaseGraphicsContextType, BaseObjectType, BasePhysicalRepresentationType>::callUpdate(BaseObjectType* const object, float dt) {
	(static_cast<RendererImplementationType*>(this))->update(this->graphicsContext.get(), object, dt);
}

template <
	typename RendererImplementationType,
	typename BaseGraphicsContextType,
	typename BaseObjectType,
	typename BasePhysicalRepresentationType
	>
void Sx::Graphics::Rendering::RendererBase<RendererImplementationType, BaseGraphicsContextType, BaseObjectType, BasePhysicalRepresentationType>::callRender(BaseObjectType* const object, BasePhysicalRepresentationType* const physicalRepresentation) {
	//--------------------------------------------------------------------------
	// Cast to the RendererImplementationType to select the correct render 
	// function implementation and pass the provided object and physical 
	// representation. This cast is safe due to static polymorphism (CRPT).
	//--------------------------------------------------------------------------
	(static_cast<RendererImplementationType*>(this))->render(this->graphicsContext.get(), object, physicalRepresentation);
}

#endif
